//
//  WYComposeToolBar.swift
//  WYWeibo
//
//  Created by wangyi on 16/7/22.
//  Copyright © 2016年 wangyi. All rights reserved.
//

import UIKit

/*!
*          发微博的 toolbar
*
*          @since 自身不具备渲染效果 依赖子控件的显示

点击按钮 要告诉控制器 你干了什么事情 采用闭包方式
*/
//按钮的枚举 类型 枚举的类型  如果不给的话 控制器 只知道是点击了 但是不知道点击的是哪一个
enum WYComposeToolBarButtonType: Int {
    
    //图片
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case EmoTicon = 3
    case Add = 4
}

class WYComposeToolBar: UIStackView {
    
    //定义表情按钮
    var emoticonButton: UIButton?
    
    //定义点击 toolBar 按钮的闭包
    var selectedToolBarButtonClouse: ((type: WYComposeToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        //设置布局方式
        axis = .Horizontal
        //子控件填充方式
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        //记录点击的 button
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .EmoTicon)
        addChildButton("compose_add_background", type: .Add)
        
    }
    //创建按钮的格式
    private func addChildButton(imageName: String, type: WYComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        
        //获取枚举的原始值
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        //设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_Highlighted"), forState: .Highlighted)
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.adjustsImageWhenHighlighted = false
        //添加控件
        addArrangedSubview(button)
        
        return button
    }
    @objc private func buttonAction(button: UIButton) {
        //通过枚举值 获取枚举成员
        let type = WYComposeToolBarButtonType(rawValue: button.tag)
        
        //执行闭包回调 闭包从外界传过来的 不一定有值 所以要有问号
        selectedToolBarButtonClouse?(type: type!)
    }
    
    //根据外界键盘类型设置不同的图片
    func showEmoticonIcon(isEmoticon: Bool) {
    
        //如果是表情键盘类型 显示键盘 icon 
        //如果是系统键盘类型 显示键盘
        if isEmoticon {
        
           //显示键盘的 icon
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
        
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
}
